package cn.schtwz.baselib.utils;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;


public class FragmentController {

    private int containerId;
    private FragmentManager fm;
    private ArrayList<Fragment> fragments;

    private FragmentController controller;

    public int getCurrPosition() {
        return currPosition;
    }

    //当前显示的fragment
    private int currPosition = 0;


    public void onDestroy() {
        controller = null;
    }

    public FragmentController(FragmentActivity activity, int containerId, ArrayList<Fragment> fragments) {
        this.containerId = containerId;
        fm = activity.getSupportFragmentManager();
        this.fragments = fragments;
        controller = this;
        initFragment();
    }

    private void initFragment() {

        //避免重复加载fragment
        if (fm.getFragments() == null || fm.getFragments().size() == 0) {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment fragment : fragments) {
                ft.add(containerId, fragment);
            }

            ft.commitAllowingStateLoss();
        } else {
            fragments = (ArrayList<Fragment>) fm.getFragments();
        }

        currPosition = 0;

    }

    public void showFragment(int position) {
        hideFragments();
        Fragment fragment = fragments.get(position);
        FragmentTransaction ft = fm.beginTransaction();
        ft.show(fragment);
        ft.commitAllowingStateLoss();
        currPosition = position;
    }

    public void hideFragments() {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                ft.hide(fragment);
            }
        }
        ft.commitAllowingStateLoss();
    }

    public Fragment getFragment(int position) {
        return fragments.get(position);
    }
}